package com.pmsystem.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.pmsystem.pojo.RoomKey;

import com.pmsystem.service.RoomKeyService;
import com.pmsystem.util.BeanUtil;

@Controller
@RequestMapping(value="admin")
public class RoomKeyController {

	@Autowired
	private RoomKeyService roomKeyService;
	
	/**
	 * 
	 * <p>Title: addRoomKey</p >  
	 * <p>Description: 管理员添加钥匙信息</p >  
	 * @author 泽强
	 * @param key
	 * @return
	 */
	@RequestMapping(value="addRoomKey",method=RequestMethod.POST)
	@ResponseBody
	public String addRoomKey(@RequestParam Map<String,Object> map) {
		RoomKey roomKey = BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map), RoomKey.class);
		boolean flag = roomKeyService.addRoomkey(roomKey);
		if(flag) {
			return "success";
		}
		return "error";
	}
	
	/**
	 * 
	 * <p>Title: updateRoomKey</p >  
	 * <p>Description: 管理员修改钥匙信息</p >  
	 * @author 泽强
	 * @param key
	 * @return
	 */
	@RequestMapping(value="updateRoomKey",method=RequestMethod.POST)
	@ResponseBody
	public String updateRoomKey(@RequestParam Map<String,Object> map) {
		RoomKey roomKey = BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map), RoomKey.class);
		boolean flag = roomKeyService.update(roomKey);
		if(flag) {
			return "success";
		}
		return "error";
	}
	
	/**
	 * 
	 * <p>Title: CountRoomKey</p >  
	 * <p>Description: 分页查询房间的钥匙数</p >  
	 * @author 泽强
	 * @param map
	 * @return 钥匙数、房间Id、房间名、房间所属用户的id和名字
	 */
	@RequestMapping(value="countRoomKey",method=RequestMethod.GET)
	@ResponseBody
	public Object countRoomKey(@RequestParam Map<String,Object> map) {	
		return roomKeyService.countRoomKey(map);	
	}
	
	/**
	 * 
	 * <p>Title: findKey</p >  
	 * <p>Description: 按一定条件查询钥匙,注意SQL语句</p >  
	 * @author 泽强
	 * @param map
	 * @return
	 */
	@RequestMapping(value="findRoomKey",method=RequestMethod.GET)
	@ResponseBody
	public Object findRoomKey(@RequestParam Map<String,Object> map) {		
		List<RoomKey> list=roomKeyService.findRoomKey(map);	
		return list;
	}
	
	
	@RequestMapping(value="findRoomKeyAndRoomAndUser",method=RequestMethod.GET)
	@ResponseBody
	public Object findRoomKeyAndRoomName(@RequestParam Map<String, Object> map) {
/*		测试语句
		Map<String, Object> map = new HashMap<>();
		map.put("enable" , "1");
		List<String> keyIdList = new ArrayList<>();
		keyIdList.add("6");
		keyIdList.add("4");
		map.put("keyIdList", keyIdList);
*/	
		return roomKeyService.findRoomKeyAndRoomAndUser(map);
	}
}
